import {
	API_PREFIX,
	API_PREFIX_CORE
} from '../config/env.js'
import http from '../utils/http.js'

export const API = {
	POST_USER_LOGIN: `${API_PREFIX_CORE}/api/v1/hy/passport/login`,
	POST_USER_PASSPORT_REGISTER: `${API_PREFIX_CORE}/api/v1/hy/passport/register`,
	POST_USER_PASSPORT_REGISTER_CARD: `${API_PREFIX_CORE}/api/v1/hy/passport/retrieve_card`,
	POST_USER_PASSPORT_FORGET_PWD: `${API_PREFIX_CORE}/api/v1/hy/passport/forget_pwd`,
	POST_USER_PASSPORT_FORGET_PAY_PWD: `${API_PREFIX_CORE}/api/v1/hy/passport/forget_pay_pwd`,
	POST_USER_SENDSMS_REGISTER: `${API_PREFIX_CORE}/api/v1/common/sendSms/register`,
	POST_USER_SENDSMS_RETRIEVECARD: `${API_PREFIX_CORE}/api/v1/common/sendSms/retrieveCard`,
	POST_USER_SENDSMS_FORGETPWD: `${API_PREFIX_CORE}/api/v1/common/sendSms/forgetPwd`,
	POST_USER_SENDSMS_CHANGEPWD: `${API_PREFIX_CORE}/api/v1/common/sendSms/changePwd`,
	POST_USER_SENDSMS_CHANGEPAYPWD: `${API_PREFIX_CORE}/api/v1/common/sendSms/changePayPwd`,
	POST_USER_SENDSMS_CHANGEPAYMENT: `${API_PREFIX_CORE}/api/v1/common/sendSms/changePayment`,
	GET_USER_INFO_CUR: `${API_PREFIX_CORE}/api/v1/hy/info/cur`,
	POST_STORE_UPLOAD: `${API_PREFIX_CORE}/api/v1/common/store/upload`,
	PUT_USER_INFO_PIC: `${API_PREFIX_CORE}/api/v1/hy/info/pic`,
	PUT_USER_INFO_PWD_LOGIN: `${API_PREFIX_CORE}/api/v1/hy/info/pwd/login`,
	PUT_USER_INFO_PWD_PAY: `${API_PREFIX_CORE}/api/v1/hy/info/pwd/pay`,
	
}

export const STORE = {
	TOKEN: 'TOKEN', // 存储用户jwt-token
	USERINFO: 'userInfo', // 用户信息
	WALLETLIST: 'walletList', // 用户钱包列表
}

/**
 * 登录
 */
export const postUserLogin = function(data, config) {
	return http.post(API.POST_USER_LOGIN, data, config, true)
}

/**
 * 注册
 */
export const postUserPassportRegister = function(data, config) {
	return http.post(API.POST_USER_PASSPORT_REGISTER, data, config, true)
}

/**
 * 找回会员卡
 */
export const postUserPassportRegisterCard = function(data, config) {
	return http.post(API.POST_USER_PASSPORT_REGISTER_CARD, data, config, true)
}

/**
 * 修改/忘记密码
 */
export const putUserPassportForgetPwd = function(data, config) {
	return http.put(API.POST_USER_PASSPORT_FORGET_PWD, data, config, true)
}

/**
 * 修改/忘记支付密码
 */
export const putUserPassportForgetPayPwd = function(data, config) {
	return http.put(API.POST_USER_PASSPORT_FORGET_PAY_PWD, data, config, true)
}

/**
 * 注册验证码
 */
export const postUserSendsmsRegister = function(data, config) {
	return http.post(API.POST_USER_SENDSMS_REGISTER, data, config)
}

/**
 * 找回会员卡验证码
 */
export const postUserSendsmsRetrievecard = function(data, config) {
	return http.post(API.POST_USER_SENDSMS_RETRIEVECARD, data, config)
}

/**
 * 忘记密码验证码
 */
export const postUserSendsmsForgetpwd = function(data, config) {
	return http.post(API.POST_USER_SENDSMS_FORGETPWD, data, config)
}

/**
 * 修改登录密码验证码
 */
export const postUserSendsmsChangepwd = function(data, config) {
	return http.post(API.POST_USER_SENDSMS_CHANGEPWD, data, config)
}

/**
 * 修改支付登录密码验证码
 */
export const postUserSendsmsChangePayPwd = function(data, config) {
	return http.post(API.POST_USER_SENDSMS_CHANGEPAYPWD, data, config)
}

/**
 * 修改支付方式验证码
 */
export const postUserSendsmsChangepayment = function(data, config) {
	return http.post(API.POST_USER_SENDSMS_CHANGEPAYMENT, data, config)
}


/**
 * 上传图片
 */
export const postStoreUpload = function(data, config) {
	return http.post(API.POST_STORE_UPLOAD, data, config)
}

/**
 * 更新头像
 */
export const putUserInfoPic = function(data, config) {
	return http.put(API.PUT_USER_INFO_PIC, data, config, true)
}

/**
 * 修改密码
 */
export const putUserInfoPwdLogin = function(data, config) {
	return http.put(API.PUT_USER_INFO_PWD_LOGIN, data, config, true)
}

/**
 * 修改支付密码
 */
export const putUserInfoPwdPay = function(data, config) {
	return http.put(API.PUT_USER_INFO_PWD_PAY, data, config, true)
}

/**
 * 获取用户登录凭证
 * @return {String} token 用户凭证
 */
export const postUserLoginToken = function() {
	let token = ''
	try {
		token = uni.getStorageSync(STORE.TOKEN)
	} catch (e) {
		token = ''
	}
	return token
}

/**
 * 设置token
 * @param {String} token 
 */
export const setUserToken = function(token) {
	try {
		uni.setStorageSync(STORE.TOKEN, token)
	} catch (e) {}
}

/**
 * 删除token
 */
export const deleteUserLoginToken = function() {
	try {
		uni.removeStorageSync(STORE.TOKEN)
		uni.removeStorageSync(STORE.USERINFO)
		uni.removeStorageSync(STORE.WALLETLIST)
	} catch (e) {}
}

/**
 * 获取当前用户信息
 */
export const getUserInfoCur = function(data, config, isReset) {
	// let userInfo = {}
	// try {
	// 	userInfo = uni.getStorageSync(STORE.USERINFO)
	// } catch (e) {}
	// console.log(JSON.stringify(data) , JSON.stringify(userInfo.apiData))
	// if(
	// 	!isReset &&
	// 	userInfo &&
	// 	userInfo.data &&
	// 	JSON.stringify(data) === JSON.stringify(userInfo.apiData) &&
	// 	JSON.stringify(config) === JSON.stringify(userInfo.apiConfig)
	// ){
	// 	return new Promise((resolve, reject)=>{
	// 		resolve(userInfo)
	// 	})
	// }
	// return http.get(API.GET_USER_INFO_CUR, data, config).then((res)=>{
	// 	uni.setStorageSync(STORE.USERINFO, {...res, apiData: data, apiConfig: config})
	// 	return res
	// })
	return handApiCache({
		STOREKey: STORE.USERINFO,
		ApiUrl: API.GET_USER_INFO_CUR,
		data,
		config,
		isReset,
		isLoading: true
	})
}

export const handApiCache = ({
	STOREKey,
	ApiUrl,
	data,
	config,
	isReset,
	isLoading
}) => {
	let resData = {}
	try {
		resData = uni.getStorageSync(STOREKey)
	} catch (e) {}
	if(
		!isReset &&
		resData &&
		resData.data &&
		JSON.stringify(data) === JSON.stringify(resData.apiData) &&
		JSON.stringify(config) === JSON.stringify(resData.apiConfig)
	){
		return new Promise((resolve, reject)=>{
			resolve(resData)
		})
	}
	return http.get(ApiUrl, data, config, isLoading).then((res)=>{
		uni.setStorageSync(STOREKey, {...res, apiData: data, apiConfig: config})
		return res
	})
}
